/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sleep;

import java.util.Timer;
import java.util.TimerTask;
import plugin.PluginInterface;

/**
 *
 * @author ZeM
 */
public class Main implements PluginInterface {

    static boolean stop;
    static String Seconds;




    public boolean hasParameters() {
        return true;
    }

    public String[] ParameterNames() {
        String[] ret = {"seconds"};
        return ret;
    }

    public String[] GroupNames() {
        return null;
    }

    public String[][] GroupParameterNames() {
        return null;
    }

    public String About() {
        return "Sleep - waiting for specified amount of seconds";
    }

    public boolean Crashed() {
        return false;
    }

    public String CrashError() {
        return null;
    }

    public void Start() {

        stop = false;
        working=true;
        TimerRun(Seconds);

        SecondTimerRun();

    }

    public void Stop() {
        stop=true;
    }
    Timer timer;
    Timer everysecondtimer;
    boolean working;

    private void TimerRun(String string) {
        int seconds = Integer.parseInt(string);
        timer = new Timer();
        timer.schedule(new Alarm(), seconds * 1000);
    }

    public void InputParameters(String[][] Plugin_String_parameter, String[][][][] Plugin_Group_String_parameter) {
        Seconds=Plugin_String_parameter[0][0];
    }

    class Alarm extends TimerTask {

        public void run() {
            if (!stop) {
                stop = false;
                notifyAll();
            }
            working=false;
            timer.cancel(); //Terminate the timer thread
        }
    }

    private void SecondTimerRun() {
        everysecondtimer = new Timer();


        everysecondtimer.schedule(new Check(), 500, 500);

    }

    class Check extends TimerTask {

        public void run() {

            if (stop) {


                timer.cancel(); //if stop parameter is true
                everysecondtimer.cancel();
            }
            if (!working) {
                everysecondtimer.cancel();
            }
        }
    }
}
